package weaver.formmode.customjavacode.modeexpand.crm;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.MyMD5;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;


/**
 * 供应商注册完成，点击保存后，insert数据到hrmresource
 * @author linxing
 * @version 2020年10月22日 13:27:13
 *
 */
public class CreateNewAccount extends AbstractModeExpandJavaCodeNew {
    /**
     * 执行模块扩展动作
     * @param param
     *  param包含(但不限于)以下数据
     *  user 当前用户
     *  importtype 导入方式(仅在批量导入的接口动作会传输) 1 追加，2覆盖,3更新，获取方式(int)param.get("importtype")
     *  导入链接中拼接的特殊参数(仅在批量导入的接口动作会传输)，比如a=1，可通过param.get("a")获取参数值
     *  页面链接拼接的参数，比如b=2,可以通过param.get("b")来获取参数
     * @return
     */
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        BaseBean log = new BaseBean();
        Map<String, String> result = new HashMap<String, String>();
        try {
            User user = (User)param.get("user");
            int billid = -1;//数据id
            int modeid = -1;//模块id

            String logindid = "";
            String password = "";
            String xm = "";
            String xb = "0";
            String yx = "";
            String departmentid = "";
            String subcompanyid1 = "";
            String mobile = "";
            String qymc = "";//企业名称，默认添加分部也是这个名称
            String dwmcxm = "";//个人名称，当选择是个人的时候填写
            int maxId = 0;

            MyMD5 md5 = new MyMD5();
            RequestInfo requestInfo = (RequestInfo)param.get("RequestInfo");
            if(requestInfo!=null){
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());
                if(billid>0&&modeid>0){
                    String sql = "select * from uf_SRM_gyszc where id="+billid;
                    RecordSet rs = new RecordSet();
                    rs.execute(sql);
                    while(rs.next()){
                        logindid = rs.getString("dlzh");
                        String mm = rs.getString("mm");
                        password = md5.createPassWord(mm);
                        xm = rs.getString("xm");
                        xb = rs.getString("xb");
                        departmentid = rs.getString("gyslx");
                        mobile = rs.getString("sjhm");
                        yx = rs.getString("yx");
                        qymc = rs.getString("qymc");
                        dwmcxm = rs.getString("dwmcxm");
                    }

                    String sql3 = "select max(id) as maxid from hrmresource";//查找hrmresource中的最大ID
                    RecordSet rs3 = new RecordSet();
                    rs3.execute(sql3);
                    while(rs3.next()){
                        maxId = Integer.parseInt(rs3.getString("maxid"));
                        maxId++;
                    }


                    //插入分部 其中 2021是供应商分部
                    String sql7 = "insert into hrmsubcompany(subcompanyname,subcompanydesc,companyid,supsubcomid)values('"+qymc+"','"+qymc+"',1,2021)";
                    RecordSet rs7 = new RecordSet();
                    rs7.execute(sql7);

                    String sql4 = "select id from hrmsubcompany where subcompanyname='"+qymc+"'";
                    RecordSet rs4 = new RecordSet();
                    rs4.execute(sql4);
                    while(rs4.next()){
                        subcompanyid1 = rs4.getString("id");
                    }

                    //插入部门
                    if(!"".equals(qymc)){
                        String sql8 = "insert into hrmdepartment(departmentmark,departmentname,subcompanyid1,supdepid)values";
                        sql8 += "(";
                        sql8 += "'"+qymc+"',";
                        sql8 += "'"+qymc+"',";
                        sql8 += subcompanyid1+",0";
                        sql8 += ")";
                        RecordSet rs8 = new RecordSet();
                        rs8.execute(sql8);

                        String sql9 = "select max(id)as id from hrmdepartment";
                        RecordSet rs9 = new RecordSet();
                        rs9.execute(sql9);
                        while(rs9.next()){
                            departmentid = rs9.getString("id");
                        }
                    }else{//默认已经新建了 个人 这个部门
                        String sql10 = "select id from hrmdepartment where departmentname='个人'";
                        RecordSet rs10 = new RecordSet();
                        rs10.execute(sql10);
                        while(rs10.next()){
                            departmentid = rs10.getString("id");
                        }
                    }
                    String sql2 = "insert into hrmresource(id,loginid,password,lastname,sex,email,departmentid,subcompanyid1,mobile,status) values";
                    sql2 += "(";
                    sql2 += maxId+",";
                    sql2 += "'"+logindid+"',";
                    sql2 += "'"+password+"',";
                    if("".equals(qymc)){
                        sql2 += "'"+xm+"',";
                    }else{
                        sql2 += "'"+qymc+"',";
                    }

                    sql2 += xb+",";
                    sql2 += "'"+yx+"',";
                    sql2 += departmentid+",";
                    sql2 += subcompanyid1+",";
                    sql2 += "'"+mobile+"',";
                    sql2 += "1)";
                    RecordSet rs2 = new RecordSet();
                    rs2.execute(sql2);

                    //add by linx at 2020-10-29 21:37:43 如果是个人 直接往供应商卡片新增加一条记录
                    //TODO
                }
            }
        } catch (Exception e) {
            result.put("errmsg","自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }

}